class Ajax {
    static post(url, data) {
        return Ajax.all('post', url, data)
    }
    static get(url, data, success) {
        return Ajax.all('get', url, data)
    }
    static all(method, url, data) {
        // console.log(data);
        if (!url) {
            throw Error('请输入地址')
        }
        let params = null
        if (data) {
            params = [];
            for (let a in data) {
                params.push(`${a}=${data[a]}`)

            }
            params = params.join('&')
            // console.log(params);
            //
        }
        //  console.log(params);

        if (method == 'get' && data) {
            url += '?' + params;
            // console.log(url);
            params = null;
        }
        return new Promise((resolve, reject) => {
            let xhr = new XMLHttpRequest()
            xhr.open(method, url)
            method == 'post' && xhr.setRequestHeader('content-type',
                'application/x-www-form-urlencoded');
            xhr.send(params)

            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        resolve(xhr.response)
                    } else {
                        reject('服务器错误')
                    }
                }
            }
        })

    }
}

function $(tag) {
    return document.querySelector(tag);
}